map with string is broken?[solved]
Posted
by
teritriano
on Stack Overflow
See other posts from Stack Overflow
or by teritriano
Published on 2010-12-27T13:13:03Z
Indexed on
2010/12/28
11:54 UTC
Read the original article
Hit count: 127
Yes. I can't see what im doing wrong
the map is string, int
Here the method
bange::function::Add(lua_State *vm){
//userdata, function
if (!lua_isfunction(vm, 2)){
cout << "bange: AddFunction: First argument isn't a function." << endl;
return false;}
void *pfunction = const_cast<void *>(lua_topointer(vm, 2));
char key[32] = {0};
snprintf(key, 32, "%p", pfunction);
cout << "Key: " << key << endl;
string strkey = key;
if (this->functions.find(strkey) != this->functions.end()){
luaL_unref(vm, LUA_REGISTRYINDEX, this->functions[strkey]);}
this->functions[strkey] = luaL_ref(vm, LUA_REGISTRYINDEX);
return true;
Ok, when the code is executed...
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6e6caa9 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >
::compare(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const () from /usr/lib/libstdc++.so.6
Seriously, what's wrong with my code. Thanks for help.
Edit 1:
Ok, I've done the solution and still fails. I've tried directly insert a string but gives the same error.
Let's see, the object is a bange::scene
inherited from bange::function
. I create the object with lua_newuserdata
:
bange::scene *scene = static_cast<bange::scene *>(lua_newuserdata(vm, sizeof(bange::scene)));
(...)
scene = new (scene) bange::scene(width, height, nlayers, vm);
I need this for LUA garbage collection. Now the access to bange::function::Add
from Lua:
static int bangefunction_Add(lua_State *vm){
//userdata, function
bange::function *function = reinterpret_cast<bange::function *>(lua_touserdata(vm, 1));
cout << "object with bange::function: " << function << endl;
bool added = function->bange::function::Add(vm);
lua_pushboolean(vm, static_cast<int>(added));
return 1;
}
Userdata is bange::scene
stored in Lua. Knowing that userdata is scene, in fact, the object's direction is the same when I've created the scene before. I need the reinterpret_cast, and then call the method. The pointer "this" is still the same direction inside the method.
solved
I did a small test in the bange::function
constructor which works without problems.
bange::function::function(){
string test("test");
this->functions["test"] = 2;
}
I finally noticed that the problem is
bange::function *function = reinterpret_cast<bange::function *>(lua_touserdata(vm, 1));
because the object is bange::scene
and no bange::function
(i admit it, a pointer corruption) and this seems more a code design issue. So this, in a way, is solved. Thanks everybody.
© Stack Overflow or respective owner